package com.ruyuan.seckilling.publish.listener;

import com.alibaba.fastjson.JSONObject;
import com.ruyuan.seckilling.common.ChannelKey;
import com.ruyuan.seckilling.common.QueueKey;
import com.ruyuan.seckilling.publish.async.event.DownloadEvent;
import com.ruyuan.starter.async.BossEventBus;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author zhonghuashishan
 */
@Slf4j
@Component
@RocketMQMessageListener(
        topic = QueueKey.QUEUE_PUBLISH_PAGE,
        consumerGroup = "publishPageGroup",
        messageModel = MessageModel.BROADCASTING)
public class PublishPageListener implements RocketMQListener<String> {

    @Autowired
    private BossEventBus bossEventBus;

    // 消息格式示例
    // .pageLogId(context.getPageLog().getId())
    // .staticPageUrl(context.getStaticPageUrl())
    // .staticPagePath(staticPagePath)
    @Override
    public void onMessage(String messageString) {

        log.info("收到发布页面的消息, message={}", messageString);

        JSONObject message = JSONObject.parseObject(messageString);
        DownloadEvent event = new DownloadEvent();
        event.setPageLogId(message.getLong("pageLogId"));
        event.setStaticPageId(message.getString("staticPageId"));
        event.setStaticPagePath(message.getString("staticPagePath"));
        bossEventBus.publish(ChannelKey.CHANNEL_DOWNLOAD, event, null);
    }
}
